import 'package:aio/aio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/config/app_data_config.dart';
import 'package:app/src/http/bean/wroks/search_bean.dart';
import 'package:app/src/ui/app_ui.dart';
import 'package:app/src/http/bean/ywx_limit.dart';

import '../../../../manager/ywx_manager.dart';
import 'public/review_list_material.dart';

///物料请购
class MaterialRequisitionRoute extends StatefulWidget {
  YwxItem item;

  MaterialRequisitionRoute(this.item);

  @override
  _State createState() => _State();
}

class _State extends State<MaterialRequisitionRoute> {
  _Presenter presenter = _Presenter();

  @override
  void initState() {
    super.initState();
    // print(
    //     "搜索条件菜单（开始）初始化：==============================================${presenter.searchInfo}");
    if (presenter.searchInfo == null) {
      presenter.searchInfo = SearchInfo(widget.item);
      presenter.searchInfo?.factory = AppDataConfig.getFactory(); //获取当前默认工厂
    }
    // widget?.item?.ReviewType = ReviewType
    //     .Contract; //=============================fixme 这里用合同审核作为测试数据；后面记得要改过哦。
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    widget?.item?.ReviewType = ReviewType
        .MaterialRequisition; //=============================fixme 恢复一下
  }

  ///是否切换了菜单
  bool isSwitchMenu = false;

  initSearchInfo(BuildContext context) {
    isSwitchMenu = true;
    // print(
    //     "搜索条件菜单切换：==============================================${presenter.searchInfo}");
    if (presenter.searchInfo != null) {
      presenter.searchInfo!
        ..isShowStatus = false //审核状态
        ..isShowRequisitionStatus = true; //fixme 显示物料请购
      presenter.searchInfo?.RequisitionStatus =
          widget.item.ReviewType2 ?? ""; //物料请购
      if (widget.item.ReviewType2 ==
          ReviewType.MaterialRequisition_OriginalRequisition) {
        //原片请购
        presenter.searchInfo!..isShowFactory = true; //所属工厂
        presenter.searchInfo!..isDelFactory = false;
        presenter.searchInfo!..isShowSpecies = true; //种类
        presenter.searchInfo!.SpeciesName =
            AppStrings.of(context).Species0; //原片种类
        presenter.searchInfo!..isShowBrand = true; //品牌
        presenter.searchInfo!.BrandName = AppStrings.of(context).Brand0; //原片品牌
        presenter.searchInfo!..isShowMaterialName = true; //名称
        presenter.searchInfo!.PreMaterialName =
            AppStrings.of(context).MaterialName0; //原片名称
        presenter.searchInfo!.isShowAddTime = false; //日期
        presenter.searchInfo!.isShowTypeOfMaterial = false; //物料类型
        presenter.searchInfo!.isShowPurchaseOrderNumber = false; //请购单号
        presenter.searchInfo!.isShowProjectName = false; //项目名称
        presenter.searchInfo!.isShowFounder = false; //创建人
        presenter.searchInfo!.isShowPurchaseStatus = false; //状态
      } else if (widget.item.ReviewType2 ==
          ReviewType.MaterialRequisition_AccessoriesRequisition) {
        //辅料请购
        presenter.searchInfo!..isShowFactory = true; //所属工厂
        presenter.searchInfo!..isDelFactory = false;
        presenter.searchInfo!..isShowSpecies = true; //种类
        presenter.searchInfo!.SpeciesName =
            AppStrings.of(context).Species1; //辅料种类
        presenter.searchInfo!..isShowBrand = true; //品牌
        presenter.searchInfo!.BrandName = AppStrings.of(context).Brand1; //辅料品牌
        presenter.searchInfo!..isShowMaterialName = true; //名称
        presenter.searchInfo!.PreMaterialName =
            AppStrings.of(context).MaterialName1; //辅料名称
        presenter.searchInfo!.isShowAddTime = false; //日期
        presenter.searchInfo!.isShowTypeOfMaterial = false; //物料类型
        presenter.searchInfo!.isShowPurchaseOrderNumber = false; //请购单号
        presenter.searchInfo!.isShowProjectName = false; //项目名称
        presenter.searchInfo!.isShowFounder = false; //创建人
        presenter.searchInfo!.isShowPurchaseStatus = false; //状态
      } else if (widget.item.ReviewType2 ==
          ReviewType.MaterialRequisition_HardwareRequisition) {
        //五金请购
        presenter.searchInfo!..isShowFactory = true; //所属工厂
        presenter.searchInfo!..isDelFactory = false;
        presenter.searchInfo!..isShowSpecies = true; //种类
        presenter.searchInfo!.SpeciesName =
            AppStrings.of(context).Species2; //五金种类
        presenter.searchInfo!..isShowBrand = true; //品牌
        presenter.searchInfo!.BrandName = AppStrings.of(context).Brand2; //五金品牌
        presenter.searchInfo!..isShowMaterialName = true; //名称
        presenter.searchInfo!.PreMaterialName =
            AppStrings.of(context).MaterialName2; //五金名称
        presenter.searchInfo!.isShowAddTime = false; //日期
        presenter.searchInfo!.isShowTypeOfMaterial = false; //物料类型
        presenter.searchInfo!.isShowPurchaseOrderNumber = false; //请购单号
        presenter.searchInfo!.isShowProjectName = false; //项目名称
        presenter.searchInfo!.isShowFounder = false; //创建人
        presenter.searchInfo!.isShowPurchaseStatus = false; //状态
      } else if (widget.item.ReviewType2 ==
          ReviewType.MaterialRequisition_Generated) {
        //已生成
        presenter.searchInfo!..isShowFactory = false; //所属工厂
        presenter.searchInfo!
          ..isDelFactory = false; //是否可以删除所属工厂，这里也要设置不删除；防止清空的时候，把工厂清空。
        presenter.searchInfo!..isShowSpecies = false; //种类
        presenter.searchInfo!..isShowBrand = true; //品牌
        presenter.searchInfo!.BrandName = AppStrings.of(context).Brand; //品牌
        presenter.searchInfo!..isShowMaterialName = false; //名称
        presenter.searchInfo!.isShowAddTime = true; //日期
        presenter.searchInfo!.showAddTime =
            AppStrings.of(context).PurchaseDate; //采购要货日期
        presenter.searchInfo!.isShowTypeOfMaterial = true; //物料类型
        presenter.searchInfo!.isShowPurchaseOrderNumber = true; //请购单号
        presenter.searchInfo!.isShowProjectName = true; //项目名称
        presenter.searchInfo!.isShowFounder = true; //创建人
        presenter.searchInfo!.isShowPurchaseStatus = true; //状态
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    px.init(context);
    initSearchInfo(context);
    var scaffold = AppUi.getScaffoldWithReView(
        context, _body(context), widget.item, presenter.searchInfo!, () {
      //print("===============================================================搜索刷新");
      presenter.setStateBody?.call(() {});
    });
    return WillPopScope(
      child: scaffold,
      onWillPop: () async {
        return true; //true 不屏蔽返回键
      },
    );
  }

  Widget _body(BuildContext context) {
    //print("===============================================================页面刷新:$isShowPopuMenuWithSearch");
    return StatefulBuilder(
        builder: (BuildContext context, StateSetter setState) {
      presenter.setStateBody = setState;
      List<Widget> children = [];
      //搜索条件
      var searchLayout = AppUi.getSearchLayout(
          context,
          presenter.searchInfo!,
          () {
            presenter.setStateBody?.call(() {});
          },
          ywxItem: widget.item,
          ywxItemCallback: () {
            presenter.setStateBody?.call(() {
              //fixme 原片请购；辅料请购；五金请购，已生成 选择发生改变。
              presenter?.searchInfo?.Species = null; //种类
              presenter?.searchInfo?.Brand = null; //品牌
              presenter?.searchInfo?.MaterialName = null; //物料名称
              //以下是已生成的，算了全都清除重置好了。
              presenter.searchInfo?.TypeOfMaterial =
                  null; //物料类型,只有已生成里有，所以不用清除。
              presenter?.searchInfo?.PurchaseOrderNumber = null; //请购单号
              presenter.searchInfo!.ProjectName = null; //项目名称
              presenter.searchInfo!.FounderName = null; //创建人
              presenter.searchInfo!.FounderId = null; //创建人
              presenter.searchInfo!.PurchaseStatus = null; //状态
              presenter.searchInfo!.InsertStartDatetime = null; //采购要货日期
              presenter.searchInfo!.InsertEndDatetime = null;
              initSearchInfo(context);
            });
          });
      children.add(searchLayout);
      //审核列表
      var reviewList =
          ReviewList2(widget.item, presenter.searchInfo!, true, isSwitchMenu);
      if (isSwitchMenu) {
        isSwitchMenu = false;
      }
      children.add(Expanded(child: reviewList));
      return Column(
        children: children,
      );
    });
  }
}

class _Presenter {
  ///搜索条件
  SearchInfo? searchInfo = null;

  ///body视图状态
  StateSetter? setStateBody;
}
